* Prepare dummies for discretized baseline outcomes of math score
* All grades 
foreach i of numlist 20(20)80{
		egen quitile_score`i'=pctile(irt_score_std_b), p(`i')
}

gen quitile1_all=irt_score_std_b<=quitile_score20
local j=2
foreach i of numlist 20(20)60{
		local k=`i'+20
		gen quitile`j'_all=irt_score_std_b>quitile_score`i' & irt_score_std_b<=quitile_score`k'
		local ++j
}
gen quitile5_all=irt_score_std_b>quitile_score80 & irt_score_std_b<.

* Each grade  
foreach g of numlist 3/5{
	foreach i of numlist 20(20)80{
			egen quitile_score`i'_g`g'=pctile(irt_score_std_b) if Grade==`g', p(`i')
		}

	gen quitile1_g`g'=irt_score_std_b<=quitile_score20_g`g'
	local j=2
	foreach i of numlist 20(20)60{
			local k=`i'+20
			gen quitile`j'_g`g'=irt_score_std_b>quitile_score`i'_g`g' & irt_score_std_b<=quitile_score`k'_g`g'
			local ++j
		}
	gen quitile5_g`g'=irt_score_std_b>quitile_score80_g`g' & irt_score_std_b<.
}

* Variables for Athey and Imbens' control 
foreach var of varlist letter_b word_b para_b story_b ///
math_Q1_40_std_b math_Q41_44_std_b irt_score_base irt_score_std_b ///
student_age_b student_sex_b repeat_b Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 num_stu_tot hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5 ///
quitile2_all-quitile5_all{
	qui su `var'
	gen `var'_dota=`var'-r(mean)
	gen  Treat_`var'_dota=treat*`var'_dota
}

foreach var of varlist letter_b word_b para_b story_b ///
math_Q1_40_std_b math_Q41_44_std_b irt_score_base irt_score_std_b ///
student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T3 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen ///
quitile2_g3-quitile5_g3{
	qui su `var' if Grade==3
	gen `var'_dot3=`var'-r(mean)
	gen  Treat_`var'_dot3=treat*`var'_dot3
}

foreach var of varlist letter_b word_b para_b story_b ///
math_Q1_40_std_b math_Q41_44_std_b irt_score_base irt_score_std_b ///
student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T4 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen ///
quitile2_g4-quitile5_g4{
	qui su `var' if Grade==4
	gen `var'_dot4=`var'-r(mean)
	gen  Treat_`var'_dot4=treat*`var'_dot4
}

foreach var of varlist letter_b word_b para_b story_b ///
math_Q1_40_std_b math_Q41_44_std_b irt_score_base irt_score_std_b ///
student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen ///
quitile2_g5-quitile5_g5{
	qui su `var' if Grade==5
	gen `var'_dot5=`var'-r(mean)
	gen  Treat_`var'_dot5=treat*`var'_dot5
}

foreach var of varlist letter_b word_b para_b story_b ///
math_Q1_40_std_b math_Q41_44_std_b irt_score_base irt_score_std_b ///
student_age_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if student_sex_b==0
	gen `var'_dotm=`var'-r(mean)
	gen  Treat_`var'_dotm=treat*`var'_dotm
}

foreach var of varlist letter_b word_b para_b story_b ///
math_Q1_40_std_b math_Q41_44_std_b irt_score_base irt_score_std_b ///
student_age_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if student_sex_b==1
	gen `var'_dotf=`var'-r(mean)
	gen  Treat_`var'_dotf=treat*`var'_dotf
}


foreach var of varlist student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if beginner_b==1
	gen `var'_dL1=`var'-r(mean)
	gen  Treat_`var'_dL1=treat*`var'_dL1
}

foreach var of varlist student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if letter_b==1
	gen `var'_dL2=`var'-r(mean)
	gen  Treat_`var'_dL2=treat*`var'_dL2
}

foreach var of varlist student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if word_b==1
	gen `var'_dL3=`var'-r(mean)
	gen  Treat_`var'_dL3=treat*`var'_dL3
}

foreach var of varlist student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if para_b==1
	gen `var'_dL4=`var'-r(mean)
	gen  Treat_`var'_dL4=treat*`var'_dL4
}

foreach var of varlist student_age_b student_sex_b repeat_b num_stu_tot Multi_grade_T3_grade3 Multi_grade_T4_grade4 Multi_grade_T5_grade5 hm_incharge ratio_fonction num_classroom sch_fac_water sch_fac_toilet sch_fac_library sch_fac_kitchen grade4 grade5{
	qui su `var' if story_b==1
	gen `var'_dL5=`var'-r(mean)
	gen  Treat_`var'_dL5=treat*`var'_dL5
}
